package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JPopupMenu;
import javax.swing.JMenuItem;

import monitor.Monitor;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Observable;
import java.util.Observer;

public class Dashboard extends JFrame implements Observer {

	private JPanel contentPane;
	private JTable table;
	private JScrollPane scrollPane;
	private JPopupMenu popupMenu;
	private JMenuItem mntmStarten;
	private JMenuItem mntmBeenden;
	private JMenuItem mntmEntfernen;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Dashboard frame = new Dashboard();
					Monitor monitor = new Monitor();
					monitor.addObserver(frame);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Dashboard() {
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JLabel lblDashboard = new JLabel("Dashboard");
		lblDashboard.setFont(new Font("Tahoma", Font.BOLD, 20));
		contentPane.add(lblDashboard, BorderLayout.NORTH);
		scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (arg0.getButton() == MouseEvent.BUTTON3) {
					popupMenu.show(table, arg0.getX(), arg0.getY());
				}
			}
		});
		scrollPane.setViewportView(table);
		table.setShowVerticalLines(false);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{"Ampelgr\u00FCn", "localhost", "8080", "Server 1", "online"},
				{"Ampelrot", "localhost", "8081", "Server 2", "offline"},
				{"Ampelgelb", "localhost", "8082", "Neuer Server", "angemeldet"},
			},
			new String[] {
				"Icon", "Host", "Port", "Name", "Status"
			}
		));
		
		popupMenu = new JPopupMenu();
		contentPane.add(popupMenu, BorderLayout.SOUTH);
		
		mntmStarten = new JMenuItem("Starten");
		popupMenu.add(mntmStarten);
		
		mntmBeenden = new JMenuItem("Beenden");
		popupMenu.add(mntmBeenden);
		
		mntmEntfernen = new JMenuItem("Entfernen");
		popupMenu.add(mntmEntfernen);

	}

	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		
	}

}
